<?php
require_once '../config.php';

function createFileHTML($dir) {
	$not_file = array('index.html','index.htm','index.php');
	$check = true;
	$array_file = array();
	$files = scandir($dir);
	foreach($files as $file) {
		if($file != "." && $file != ".." && substr($file,0,1) != '.') {
			if(in_array(strtolower($file),$not_file)) {
				$array_file[] = $file;
				$check = false;
			}
		}
	}

	if($check){
		//copy file tu thu muc nay den thu muc khac
		copy(ROOT_PATH.'tool/'.'index.html', $dir.'/index.html');
	}
	return array('status'=>$check,'data'=>$array_file);
}

// Ham de qui
function listDir($dir, $depth=0) {
	$array_dir = array();
	// Mo thu muc can xu ly
	if($dh = opendir($dir)){
		// tao vong lap in ra tat ca cac tap tin trong thu muc
		while (($file = readdir($dh)) !== false)  {
			if($file != "." && $file != ".." && substr($file,0,1) != '.') {
				$len = strpos($file, '.');
				if (is_dir($dir.'/'.$file)) {
					$array_dir[] = $dir . "/" . $file;
					$inner = listDir($dir . "/" . $file);
					if(is_array($inner)) $array_dir = array_merge($array_dir, $inner);
				}
			}
		}
	}
	return $array_dir;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#"
	dir="ltr" lang="en-US">
<head>
<title>Sinh file index.html</title>
</head>
<body>
<h2>Sinh file index.html</h2>
<?php
foreach(listDir(ROOT_PATH) as $dir){
	$files = createFileHTML($dir);
	//foreach($files['data'] as $file){
	//echo '   |- '.$file.'<br/>';
	//}
	echo ($files['status']) ? '<strong>'.$dir.'<strong><br/>' : $dir.'<br/>';
}
?>
</body>
</html>
